<?php

namespace app\admin\model\xlzxinfo;

use think\Model;
use traits\model\SoftDelete;


class Xlzxinfo extends Model
{




    use SoftDelete;
    // 表名
    protected $name = 'mental_xlzxinfo';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
//        'gender_text',
//        'status_text',
            'tags_text',
            'certificate_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getGenderList()
    {
        return ['1' => __('Gender 1'), '2' => __('Gender 2')];
    }

    public function getIsExpList()
    {
        return ['0' => __('Isexp 0'), '1' => __('Isexp 1')];
    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getGenderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gender']) ? $data['gender'] : '');
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTagsTextAttr($value, $data)
    {
        if ($data['tags']){
            $tags_arr = explode(',',$data['tags']);
            $data = Tags::whereIn('id',$tags_arr)->column('name');
        }else{
            $data = [];
        }

        return $data;
    }
    public function getCertificateTextAttr($value, $data)
    {

        if ($data['certificate']){
            $certificate_arr = explode(',',$data['certificate']);
            $data = Certificate::whereIn('id',$certificate_arr)->column('name');
        }else{
            $data = [];
        }

        return $data;
    }
    public function getImageAttr($value)
    {
        return compatImage($value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
